﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;

using BuzzwordBingo.Interface;
using BuzzwordBingo.Entities;
using BuzzwordBingo.PersistenceLINQ;

namespace BuzzwordBingo.PersistenceLINQ.Test
{
    /// <summary>
    /// Summary description for PeristenceServiceFixture
    /// </summary>
    [TestFixture]
    public class PersistenceServiceFixture
    {
        private const int EVENTID = 1234;
        private const int CATEGORYID = 2345;

        [Test]
        public void SaveEvent()
        {
            BuzzwordCategory bc = CreateLinqBuzzCategory();
            DataAccessHelper.AddBuzzCategory(bc, null);
            IPersistenceService persistenceService = new PersistenceService();
            IBingoEvent theEvent = CreateBuzzwordBingoEvent();
            persistenceService.SaveEvent(theEvent);
            BingoEvent eventFromDb = DataAccessHelper.GetEvent(EVENTID);
            Assert.AreEqual(EVENTID, eventFromDb.EventId);
            Assert.AreEqual("Joe", eventFromDb.Organiser);
            DataAccessHelper.DeleteEvent(EVENTID);
            DataAccessHelper.DeleteBuzzCategory(bc.BuzzwordCategoryId);
        }

        [Test]
        public void LoadEvent()
        {
            BuzzwordCategory bc = CreateLinqBuzzCategory();
            DataAccessHelper.AddBuzzCategory(bc, null);
            BingoEvent be = CreateLinqBingoEvent();
            DataAccessHelper.AddEvent(be);
            IPersistenceService persistenceService = new PersistenceService();
            BuzzwordBingo.Entities.BingoEvent theEvent = (BuzzwordBingo.Entities.BingoEvent)persistenceService.LoadEvent(EVENTID);
            Assert.AreEqual(EVENTID, theEvent.Id);
            Assert.AreEqual("Joe", theEvent.Organiser);
            DataAccessHelper.DeleteEvent(be.EventId);
            DataAccessHelper.DeleteBuzzCategory(bc.BuzzwordCategoryId);
        }

        [Test]
        public void LoadCategoryList()
        {
            BuzzwordCategory bc = CreateLinqBuzzCategory();
            DataAccessHelper.AddBuzzCategory(bc, null);
            BuzzwordCategory bc1 = CreateLinqBuzzCategory();
            bc1.BuzzwordCategoryId = 3456;
            bc1.Category += "-1";
            DataAccessHelper.AddBuzzCategory(bc1, null);
            IPersistenceService persistenceService = new PersistenceService();
            IList<IBuzzwordCategory> myList = persistenceService.LoadBuzzwordCategoryList();
            Assert.AreEqual("TEST-CATEGORY", myList.First().Category.Substring(0, 13));
            Assert.AreEqual(2, myList.Count());

            foreach (IBuzzwordCategory cat in myList)
                Console.WriteLine("category: " + cat.Category);

            DataAccessHelper.DeleteBuzzCategory(bc.BuzzwordCategoryId);
            DataAccessHelper.DeleteBuzzCategory(bc1.BuzzwordCategoryId);
        }

        [Test]
        public void LoadCategory()
        {
            BuzzwordCategory bc = CreateLinqBuzzCategory();
            DataAccessHelper.AddBuzzCategory(bc, CreateLinqBuzzwords());           
            IPersistenceService persistenceService = new PersistenceService();
            IBuzzwordCategory myCategory = persistenceService.LoadBuzzwordCategory(bc.BuzzwordCategoryId);
            Assert.AreEqual("TEST-CATEGORY", myCategory.Category);
            Assert.AreEqual(25, myCategory.BuzzWordList.Count());

            Console.WriteLine("category: " + myCategory.Category);
            foreach (IBuzzWord bw in myCategory.BuzzWordList)
                Console.WriteLine(bw.FuzzyWord);

            DataAccessHelper.DeleteBuzzCategory(bc.BuzzwordCategoryId);
        }

        #region factory methods

        public IBingoEvent CreateBuzzwordBingoEvent()
        {
            BuzzwordBingo.Entities.BingoEvent myEvent = new BuzzwordBingo.Entities.BingoEvent();

            DateTime start = DateTime.Parse("2007-01-01 14:00");
            DateTime end = DateTime.Parse("2007-01-01 16:00");
            myEvent.AddEventDates(start, end);
            string speaker = "Mr. Foobar";
            myEvent.Speaker = speaker;
            string organiser = "Joe";
            myEvent.Organiser = organiser;

            for (int i = 0; i < 35; i++)
                myEvent.AddBuzzWord(new BuzzWord(i.ToString()));

            IParticipant p1 = new BuzzwordBingo.Entities.Participant("email@dot.com");
            IParticipant p2 = new BuzzwordBingo.Entities.Participant("email1@dot.com");
            myEvent.AddParticipant(p1);
            myEvent.AddParticipant(p2);

            BuzzwordBingo.Entities.BuzzwordCategory c = new BuzzwordBingo.Entities.BuzzwordCategory("FOO-CATEGORY");
            c.Id = CATEGORYID;
            myEvent.Category = c;

            // set known event id
            myEvent.Id = EVENTID;
            return myEvent;
        }

        public BingoEvent CreateLinqBingoEvent()
        {
            BingoEvent myEvent = new BingoEvent();

            myEvent.EventId = EVENTID;
            myEvent.BuzzwordCategoryId = CATEGORYID;
            DateTime start = DateTime.Parse("2007-01-01 14:00");
            DateTime end = DateTime.Parse("2007-01-01 16:00");
            myEvent.StartDate = start;
            myEvent.EndDate = end;
            string speaker = "Mr. Foobar";
            myEvent.Speaker = speaker;
            string organiser = "Joe";
            myEvent.Organiser = organiser;

            Participant p = new Participant();
            p.ParticipantId = 4567;
            p.Email = "email@dot.com";
            p.EventId = myEvent.EventId;
            myEvent.Participants.Add(p);

            p = new Participant();
            p.ParticipantId = 5678;
            p.Email = "email1@dot.com";
            p.EventId = myEvent.EventId;
            myEvent.Participants.Add(p);

            return myEvent;
        }

        public BuzzwordCategory CreateLinqBuzzCategory()
        {
            BuzzwordCategory bc = new BuzzwordCategory();

            bc.BuzzwordCategoryId = CATEGORYID;
            bc.Category = "TEST-CATEGORY";

            return bc;
        }

        public List<Buzzword> CreateLinqBuzzwords()
        {
            List<Buzzword> buzzList = new List<Buzzword>();

            for (int i = 0; i < 25; i++)
            {
                Buzzword bw = new Buzzword();
                bw.BuzzwordId = 6700 + i;
                bw.FuzzyWord = "FUZZ-" + i.ToString("{00}");
                buzzList.Add(bw);
            }

            return buzzList;
        }

        #endregion
    }
}
